package com.common.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.domain.entity.*;
import com.common.domain.vo.ProblemVo;
import com.common.mapper.ProblemMapper;
import com.common.service.ProblemService;
import com.common.util.SecurityUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
* @author pc
* @description 针对表【sys_point】的数据库操作Service实现
* @createDate 2023-09-02 11:17:55
*/
@Service
public class ProblemServiceImpl extends ServiceImpl<ProblemMapper, Problem>
implements ProblemService {

    @Autowired
    private ProblemMapper problemMapper;

    @Override
    public ResponseResult allProblemList(Integer pageNum, Integer pageSize, String userName, String type, Long userId) {
        PageHelper.startPage(pageNum,pageSize);

        List<ProblemVo> list =  problemMapper.allProblemList(userName,type, userId);

        PageInfo<ProblemVo> pageInfo = new PageInfo<>(list);

        pageInfo.setList(list);

        return ResponseResult.okResult(pageInfo);
    }

    @Override
    public ResponseResult insertProblem(Problem problem) {
        save(problem);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteProblem(String ids) {
        String[] split = ids.split(",");
        removeByIds(Arrays.asList(split));
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult changeStatus(Long id, String status) {
        //先找到
        LambdaQueryWrapper<Problem> wrapper = new LambdaQueryWrapper();
        wrapper.eq(Problem::getId,id);
        List<Problem> list = list(wrapper);
        //改变值
        list.get(0).setFlag(status);
        //修改
        update(list.get(0),wrapper);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult submitProblem(Problem problem) {
        problem.setUserId(SecurityUtils.getUserId());
        save(problem);
        return ResponseResult.okResult();
    }

}
